// UDPDevice.cpp
#include "UDPDevice.h"
#include <stdio.h>


UDPDevice::UDPDevice()
{
	this->destIP = NULL;
	this->destPort = 3333;
	this->server = NULL;
	this->remoteAddr = (sockaddr_in*)malloc(sizeof(sockaddr_in));
}
UDPDevice::~UDPDevice()
{
	if(this->server)
	{
		this->close();
	}
	free(this->remoteAddr);
	this->remoteAddr = NULL;
}




CrossDeviceList* UDPDevice::getDeviceList(void)
{
	CrossDeviceList* result = NULL;
	CrossDeviceList* current = NULL;
	HOSTENT * pHostEnt = GetLocalIpHOSTENT();
	char szIP[1024];
	int i = 0;
	if(pHostEnt)
	{
		while(pHostEnt->h_addr_list[i] != NULL)
		{
			sprintf(szIP, "%d.%d.%d.%d", (pHostEnt->h_addr_list[i][0] & 0xff),
				(pHostEnt->h_addr_list[i][1] & 0xff),
				(pHostEnt->h_addr_list[i][2] & 0xff),
				(pHostEnt->h_addr_list[i][3] & 0xff));
				
			sprintf(szIP, "UDP:%d.%d.%d.100:1000,HOST:%d.%d.%d.%d:13333", (pHostEnt->h_addr_list[i][0] & 0xff),
				(pHostEnt->h_addr_list[i][1] & 0xff),
				(pHostEnt->h_addr_list[i][2] & 0xff),
				(pHostEnt->h_addr_list[i][0] & 0xff),
				(pHostEnt->h_addr_list[i][1] & 0xff),
				(pHostEnt->h_addr_list[i][2] & 0xff),
				(pHostEnt->h_addr_list[i][3] & 0xff));
			
			#if DEBUG_UART_1
			printf("%s\n", szIP);
			#endif
			i++;
			CrossDeviceList* temp = (CrossDeviceList*)malloc(sizeof(CrossDeviceList));
			temp->name = (char*)malloc(strlen(szIP)+1);
			sprintf(temp->name, "%s", szIP);
			temp->next = NULL;
			if(result == NULL)
			{
				result = temp;
				current = temp;
			}
			else
			{
				current->next = temp;
				current = temp;
			}
		}	
	}
	return result;
}

bool UDPDevice::open(char* portName)
{
	if(strstr(portName, "UDP"))
	{
		int hostPort, destPort;
		char *hostIP = (char*)malloc(24);
		char *destIP = new char[24];
		//int *value = (int *) (destIP-4);
		//int *value1 = (int *) (hostIP-4);
		int IP1[4], IP2[4];
		this->close();
		
		
		
		sscanf(portName, "UDP:%d.%d.%d.%d:%d,HOST:%d.%d.%d.%d:%d", &IP1[0], &IP1[1],&IP1[2],&IP1[3],&destPort, 
			&IP2[0], &IP2[1],&IP2[2],&IP2[3],&hostPort);
		
		sprintf(hostIP, "%d.%d.%d.%d", IP2[0], IP2[1], IP2[2], IP2[3]);
		sprintf(destIP, "%d.%d.%d.%d", IP1[0], IP1[1], IP1[2], IP1[3]);
		#if DEBUG_UART_1
		printf("dest ip=%s, port=%d\n", destIP, destPort);
		printf("host ip=%s, port=%d\n", hostIP, hostPort);
		#endif
		
		this->server = new UDPServer(hostIP, hostPort);
		
		this->destIP = destIP;
		this->destPort = destPort;

		if(this->server->StartUDPServer() == false) // 开始侦听
		{
			this->close();
			return false;
		}
		
		
		
		return true;
	}
	return false;
}

bool UDPDevice::close()
{
	if(this->server)
	{		
		//printf("UDPDevice::close\n");
		this->server->StopUDPServer(); // 可选，因为析构函数会自动调用此方法
		//printf("closed StopUDPServer()\n");
		delete this->server;
		//printf("delete this->server\n");
		this->server = NULL;
		
		//printf("delete ip: %s\n", this->destIP);
		//int *value = (int *) (this->destIP-4);
		//printf("close: destIP-4 =%08x\n", *value);
		
		free((void*)this->destIP);
		//delete this->destIP;
		//printf("free(this->destIP)\n");
		this->destIP = NULL;
	}
	return true;
}



void UDPDevice::setSpeed(int speed)
{
	
}

int UDPDevice::readBuf(char * buf, int bufSize)
{
	if(this->server)
	{
		int remoteAddrLen = sizeof(sockaddr_in);
		return this->server->recv(buf, bufSize, this->remoteAddr, &remoteAddrLen);
	}	
	return 0;
}

int UDPDevice::writeBuf(char * buf, int bufSize)
{
	if(this->server)
	{
		this->server->UDPSendTo(buf, bufSize, this->destIP, this->destPort);
		return bufSize;
	}	
	return 0;
}

sockaddr_in * UDPDevice::getRemoteAddr(void)
{
	return this->remoteAddr;
}